// The Item object is the representation of a Monument.
// It basically contains an uri that allows to identify it,
// an attribute so specify if it has to be used to findn similar Monuments
// and a list of attributes belonging to the item

package models;

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.*;

import javax.persistence.*;

import org.omg.CORBA.portable.OutputStream;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.sparql.engine.http.QueryExceptionHTTP;
import com.hp.hpl.jena.sparql.syntax.ElementGroup;
import java.net.URLEncoder;
 
import play.db.jpa.*;

@Entity
public class Item extends Model{
	
	public String uri;
	
	/**
	 * SupportItem is used to specify if this Attribute is used
	 * to take as reference for the final search
	 */
	public Boolean supportItem;
	
	/**
	 * An Item has attributes associated
	 */
	@OneToMany(mappedBy="item", cascade=CascadeType.ALL)
	public List<Attribute> attributeList = new ArrayList<Attribute>();
	
	/**
	 * Constructor of an Item with empty attributes list
	 * @param uri item identifier
	 * @param supportItem specifyes if it will be used to find
	 * similar items
	 */
	public Item(String uri, Boolean supportItem){
		this.uri = uri;
		this.supportItem = supportItem;
		
	}
	
	/**
	 *  Constructor of an Item with a list of attributes
	 * @param uri item identifier
	 * @param attributeList list of attributes to be associated to the item
	 */
	public Item(String uri, List<Attribute> attributeList){
		this.uri = uri;
		this.attributeList = attributeList;
		this.supportItem = false;
	}
	
	/**
	 * Gets the content of Attribute with the given name
	 * @param name Name of the attribute 
	 * @return Attribute content
	 */
	public String getAttribute(String name) {
		Attribute a = Attribute.find("byItemAndName", this, name).first();
		if(a == null)
			return "Data not available!";
		else
			return a.content;
	}
	
	/**
	 * Gets all attributes that are of type category
	 * @return list of attributes
	 * @author Yaser
	 */
	public List<Attribute> getCategoryAttributes() {
		return Attribute.find("byItemAndName", this, "category").fetch();
	}
	
	/**
	 * Gets the Label Attribute and removes the language tag
	 * @return returns the content of the label attribute without the language tag
	 * @author Yaser
	 */
	public String toString() {
		String name = this.getAttribute("label");
		String[] split = name.split("@");
		return split[0];
	}
	
	/**
	 * Gets the Abstract Attribute and removes the language tag
	 * @return returns the abstract content without the language tag
	 * @author Yaser
	 */
	public String getAbstract() {
		String name = this.getAttribute("abstract");
		String[] split = name.split("@");
		return split[0];
	}
	
	
	/**
	 * Gets a larger version of the Thumbnail.
	 * If a larger version does not exist, gets the original picture
	 * This method works only for Wikipedia Thumbnails
	 * @param px size we want, in pixels
	 * @return returns the correct URL
	 */
	public String getPicture(int px) {
		String url = this.getAttribute("thumb");
		String test[] = url.split("/");
		String test2 = test[test.length - 1].split("px-")[1];
		String newUrl = test[0];
		try {
			
			for (int i = 1; i < test.length - 1; i++) {
				String temp = newUrl;
				newUrl = temp + "/" + URLEncoder.encode(test[i], "UTF-8");
			}
			newUrl = newUrl + "/" + px + "px-" + URLEncoder.encode(test2, "UTF-8");
		
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			URL u = new URL ( newUrl );
		
			HttpURLConnection huc =  ( HttpURLConnection )  u.openConnection (); 
				huc.setRequestMethod ("GET");
			huc.connect () ; 
			int code = huc.getResponseCode (  ) ;
			if(code == 500) {
				String[] newsplit = url.split("/");
				newUrl = "http://" + URLEncoder.encode(newsplit[2], "UTF-8");
				for(int i = 3; i < newsplit.length - 1; i++) {
					if(!newsplit[i].equals("thumb"))
						newUrl += "/" + URLEncoder.encode(newsplit[i], "UTF-8");
				}
			}
				
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		return newUrl;		
	}
}
